package com.wandian.notepad.mvvm.notepadclassifylist;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.notepad.R;
import com.wandian.notepad.adapter.NotepadClassifyManagerRecycleAdapter;
import com.wandian.notepad.adapter.NotepadGridRecycleAdapter;
import com.wandian.notepad.adapter.NotepadHistoryRecycleAdapter;
import com.wandian.notepad.bean.NoteTypeBean;
import com.wandian.notepad.databinding.NotepadActivityClassifyBinding;
import com.wandian.notepad.databinding.NotepadActivityClassifyListBinding;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description 记事本分类activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_NOTEPAD_CLASSIFY_LIST)
public class NotepadClassifyListActivity extends BaseMVVMActivity<NotepadClassifyListViewModel> implements  BaseQuickAdapter.RequestLoadMoreListener  {
    private NotepadActivityClassifyListBinding dataBinding;
    private NotepadClassifyManagerRecycleAdapter notepadClassifyRecycleAdapter;
    private String type_id="";
    private String type_title="";
    private int pageNumber = 1;
    private int pageSize = 30;
    private boolean isRefresh = false;
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.notepad_activity_classify_list);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
        Bundle bundle = getIntent().getExtras();
        if(!UIUtils.isEmpty(bundle)){
            type_id = bundle.getString("type_id");
            type_title = bundle.getString("type_title");
        }
        initViewData();
        initRecycleView();
        initClink();
        getData(true);
    }

    private void getData(boolean isRefresh){
        this.isRefresh = isRefresh;
        if(isRefresh){
            notepadClassifyRecycleAdapter.setEnableLoadMore(true);
            pageNumber = 1;
            notepadClassifyRecycleAdapter.notifyDataSetChanged();
        }else {
            pageNumber = pageNumber+1;
        }
        mViewModel.notepadList(type_id,pageNumber+"",pageSize+"",dataBinding.refreshLayout,notepadClassifyRecycleAdapter);
    }


    private void initRecycleView(){
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        dataBinding.rvClassify.setLayoutManager(layoutManager);
        notepadClassifyRecycleAdapter  = new NotepadClassifyManagerRecycleAdapter(R.layout.notepad_item_notes);
        dataBinding.rvClassify.setAdapter(notepadClassifyRecycleAdapter);
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        if(!UIUtils.isEmpty(type_title)){
            dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText(type_title);
        }else {
            dataBinding.bindSysTwoToolBar.toolBarTvTitle.setText("记事列表");
        }

        mViewModel.noteList.observe(this,listBean->{
            if(!UIUtils.isEmpty(listBean) && listBean.size()>0){

                if(listBean.size() != pageSize) notepadClassifyRecycleAdapter.setEnableLoadMore(false);
                if(isRefresh){
                    notepadClassifyRecycleAdapter.setNewData(listBean);
                }else {
                    notepadClassifyRecycleAdapter.addData(listBean);
                    notepadClassifyRecycleAdapter.loadMoreComplete();
                }
            }
        });

        dataBinding.refreshLayout.setColorSchemeResources(R.color.color_sys);
        dataBinding.refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getData(true);
            }
        });
    }


    private void initClink(){
        dataBinding.bindSysTwoToolBar.toolBarIvBack.setOnClickListener(v -> finish());
    }

    @Override
    public void onLoadMoreRequested() {
        getData(true);
    }
}
